﻿using UnityEngine;
using System.Collections;
using TNet;
using UnityEngine.UI;
using GameData;
using DG.Tweening;

[ExecuteInEditMode]
public class MenuUI : MonoBehaviour {


    public int serverTcpPort = 5127;
    public string[] serverList;

    string mAddress = "127.0.0.1";
    string mMessage = "";
    float mAlpha = 0f;

    public GameObject LoginPanel;
    public GameObject SelectPanel;

    public GameObject resBtn;
    public GameObject pickBtn;
    public GameObject lineBtn;
    public GameObject callBtn;

    public InputField UserInput;
    public InputField PassInput;
    public GameObject LoginBtn;
    public GameObject SkipBtn;

    public GameObject managerBtn;

    public GameObject exitBtn;

    public GameObject adminLoginObj;
    public GameObject adminLoginBtn;
    public GameObject backAdminBtn;



    // Use this for initialization
    void Start () {

        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        EventTriggerListener.Get(resBtn).onClick = OnRecevieBtn;
        EventTriggerListener.Get(managerBtn).onClick = OnManager;
        EventTriggerListener.Get(pickBtn).onClick = OnPick;
        EventTriggerListener.Get(lineBtn).onClick = OnLine;
        EventTriggerListener.Get(callBtn).onClick = OnCall;
        EventTriggerListener.Get(exitBtn).onClick = Onexit;
        EventTriggerListener.Get(LoginBtn).onClick = OnLogin;

        EventTriggerListener.Get(adminLoginBtn).onClick = onadminLoginBtn;

        EventTriggerListener.Get(backAdminBtn).onClick = backAdmin;

        if (GlobalVO.GetInstance().roleInfo==null)
        {
            if (Application.isPlaying)
            {
                //Tools.ResolveIPs(null);
                
                //TNManager.StartUDP(Random.Range(10000, 50000));
                //EventTriggerListener.Get(LoginBtn).onClick = OnLogin;
                EventTriggerListener.Get(SkipBtn).onClick = OnSkip;

                
                LoginPanel.transform.localScale = new Vector3(1, 1, 1);
                SelectPanel.transform.localScale = new Vector3(0, 0, 0);
                //OnMorenLogin();
               // Invoke("OnMorenLogin",0.5f);
            }

        }
        else
        {
            OnLoginResponse(GlobalVO.GetInstance().roleInfo);
        }
        if (GlobalVO.GetInstance().SettingVo!= null)
        {
            UserInput.text = GlobalVO.GetInstance().SettingVo.user;
            PassInput.text = GlobalVO.GetInstance().SettingVo.password;

        }

    }


    void Onexit(GameObject obj)
    {
        Application.Quit();
    }

    void OnLine(GameObject obj)
    {
        UtilTool.ChangeLevel("DueScene");

    }

    void OnCall(GameObject obj)
    {
        UtilTool.ChangeLevel("SaleScene");
    }



    //取号界面
    void OnRecevieBtn(GameObject obj)
    {
        UtilTool.ChangeLevel("Receive");
    }

    //管理员
    void OnManager(GameObject obj)
    {
        if (GlobalVO.GetInstance().isreqAdmin)
        {
            UtilTool.ChangeLevel("Manager");


        }
        else
        {
            //判断---管理员
            adminLoginObj.transform.localScale = new Vector3(1, 1, 1);
        }
        

    }


    void backAdmin(GameObject obj)
    {

        adminLoginObj.transform.localScale = new Vector3(0, 0, 0);


    }


    void onadminLoginBtn(GameObject obj)
    {
        string user = UserInput.text;
        string pass = PassInput.text;
        if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
        {

            return;
        }
        if (user.Equals(GlobalVO.GetInstance().roleInfo.UserInfo.username)&& pass.Equals(GlobalVO.GetInstance().roleInfo.UserInfo.password))
        {
            GlobalVO.GetInstance().isreqAdmin = true;
            UtilTool.ChangeLevel("Manager");

        }
    }

    //抽号
    void OnPick(GameObject obj)
    {
        UtilTool.ChangeLevel("PickScene");
    }


    void OnLogin(GameObject obj)
    {

        //创建
        //获取帐号
        string user = UserInput.text;
        string pass = PassInput.text;
        Debug.Log(user+" 登录 "+pass);
        if(string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
        {

            return;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.username = user;
        userInfo.password = pass;
        Cmd.LoginSend login = new Cmd.LoginSend();
        login.Send(userInfo, OnLoginResponse);


    }

    public void OnMorenLogin()
    {
        string user = "admin";
        string pass = "admin";
        if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
        {

            return;
        }
        //
        Debug.LogWarning("登录================");
        UserInfo userInfo = new UserInfo();
        userInfo.username = user;
        userInfo.password = pass;
        Cmd.LoginSend login = new Cmd.LoginSend();
        login.Send(userInfo, OnLoginResponse);

    }


    void OnLoginResponse(RoleInfo roleInfo)
    {
        if (roleInfo.ID <= 0)
        {
            Debug.Log("登录失败:" + roleInfo.Name);
            return;
        }
            
        Debug.Log("登录成功:" + roleInfo.Name);
        //判断权限
        if (roleInfo.power == "全部")
        {
            resBtn.SetActive(true);
            callBtn.SetActive(true);
            lineBtn.SetActive(true);
            pickBtn.SetActive(true);
            managerBtn.SetActive(true);

        }
        else if(roleInfo.power=="取号")
        {
            resBtn.SetActive(true);
            callBtn.SetActive(false);
            lineBtn.SetActive(false);
            pickBtn.SetActive(false);
            managerBtn.SetActive(false);
        }
        else if(roleInfo.power=="排号")
        {
            resBtn.SetActive(false);
            callBtn.SetActive(false);
            lineBtn.SetActive(true);
            pickBtn.SetActive(false);
            managerBtn.SetActive(false);

        }
        else if (roleInfo.power=="抽号")
        {
            resBtn.SetActive(false);
            callBtn.SetActive(false);
            lineBtn.SetActive(false);
            pickBtn.SetActive(true);
            managerBtn.SetActive(false);

        }
        else if (roleInfo.power == "叫号")
        {
            resBtn.SetActive(false);
            callBtn.SetActive(true);
            lineBtn.SetActive(false);
            pickBtn.SetActive(false);
            managerBtn.SetActive(false);

        }
        else if (roleInfo.power == "展示")
        {
            resBtn.SetActive(false);
            callBtn.SetActive(false);
            lineBtn.SetActive(true);
            pickBtn.SetActive(true);
            managerBtn.SetActive(false);

        }
        else if (roleInfo.power == "员工")
        {
            resBtn.SetActive(true);
            callBtn.SetActive(true);
            lineBtn.SetActive(true);
            pickBtn.SetActive(true);
            managerBtn.SetActive(false);

        }
        //
        GlobalVO.GetInstance().roleInfo = roleInfo;

        RectTransform tf = LoginPanel.GetComponent<RectTransform>();
        Tweener tw= tf.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
        

         tf = SelectPanel.GetComponent<RectTransform>();
        tf.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }



    void OnSkip(GameObject obj)
    {
        //创建
        //获取帐号

        UserInfo userInfo = new UserInfo();
        userInfo.username = "1";
        userInfo.password = "1";
        userInfo.isTourist = true;
        Cmd.LoginSend login = new Cmd.LoginSend();
        login.Send(userInfo, OnLoginResponse);
    }

	
	// Update is called once per frame
	void Update () {
        /*
        if (Application.isPlaying)
        {
            float target = (TNLobbyClient.knownServers.list.size == 0) ? 0f : 1f;
            if(TNLobbyClient.knownServers.list.size>0)
            {
                Debug.Log("aa:"+ TNLobbyClient.knownServers.list.size);
            }
            mAlpha = UnityTools.SpringLerp(mAlpha, target, 8f, Time.deltaTime);
        }
        */
    }





}
